import requests, json

server_url = 'http://192.168.1.233:8081/'

def get_rank() -> list:
    url = server_url + 'rank'
    resp = requests.get(url)
    return json.loads(str(resp.content, encoding='utf-8'))
    # 返回 [{'uid': 'abc', 'grade': 123}, {'uid': 'yunzen', 'grade': 103}] 列表

def login(uid: str)->str:
    url = server_url + 'login'
    postdatas = json.dumps({'uid': uid})
    resp = requests.post(url, data=postdatas, headers={'content-type': 'application/json'})
    return str(resp.content, encoding='utf-8')
    # 返回 seccess 字符串

def upload_grade(uid: str, grade: int) -> int:
    url = server_url + 'uploadGrade'
    postdatas = json.dumps({'uid': uid, 'grade': grade})
    resp = requests.post(url, data=postdatas, headers={'content-type': 'application/json'})
    return int(str(resp.content, encoding='utf-8'))

if __name__ == '__main__':
    url = server_url + 'hello'
    resp = requests.get(url)
    print(str(resp.content, encoding='utf-8'))
    # 返回 'Hello, MyTank!' 字符串

# ###
# GET http://localhost:8080/hello
# ###
# POST  http://localhost:8080/login
# content-type: application/json

# {
#     "uid": "yunzen"
# }
# ###
# POST  http://localhost:8080/uploadGrade
# content-type: application/json

# {
#     "uid": "yunzen",
#     "grade": 103
# }
# ### 
# GET http://localhost:8080/rank
# ###